/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package yandexfotkibackup;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author petr
 */
public class ReadXMLAllAlbomsHendler extends DefaultHandler {
    private boolean tagsInsideAlbom = false;
    private String thisElement = "";
    private boolean needToParse = false;
    private AlbomInfo currentAlbom;
    private List<AlbomInfo> collectionAlboms;

    public ReadXMLAllAlbomsHendler() {
        collectionAlboms = new ArrayList<>();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        thisElement = qName;
        switch (qName) {
            case "entry":
                tagsInsideAlbom = true;
                currentAlbom = new AlbomInfo();
               // break;
            case "id":
            case "title":
            case "f:protected":
                needToParse = true;
                break;
            //case "link":
            case "f:image-count":
                if(tagsInsideAlbom)
                    imageCountHendler(attributes);
                break;
        }
        needToParse &= tagsInsideAlbom;
        
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (qName.equalsIgnoreCase("entry")) {
            tagsInsideAlbom = false;
            getCollectionAlboms().add(currentAlbom);
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if (!needToParse) {
            return;
        }
        needToParse = false;
        if (thisElement.equalsIgnoreCase("id")) {
            StringTokenizer st = new StringTokenizer(new String(ch, start, length), ":");
            int id = 0;
            while (st.hasMoreTokens()) {
                String token = st.nextToken();
                if (!st.hasMoreTokens()) {
                    id = Integer.parseInt(token);
                }
            }
            System.out.println("Id : " + id);
            currentAlbom.setId(id);
        }
        if (thisElement.equalsIgnoreCase("title")) {
            String title = new String(ch, start, length);
            System.out.println("title : " + title);
            currentAlbom.setTitle(title);
        }

    }

    /**
     * @return the collectionAlboms
     */
    public List<AlbomInfo> getCollectionAlboms() {
        return collectionAlboms;
    }

    /**
     * @param collectionAlboms the collectionAlboms to set
     */
    public void setCollectionAlboms(List<AlbomInfo> collectionAlboms) {
        this.collectionAlboms = collectionAlboms;
    }

    private void imageCountHendler(Attributes attributes) {
        String str;
        try{
            str = attributes.getValue("value");
            if( str != null)
            {
                int count = Integer.parseInt(str );
                System.out.println("f:image-count : " + count);
                currentAlbom.setCountPhoto(count);
            }
        }catch(NumberFormatException e){
            Logger.getLogger(ReadXMLAllAlbomsHendler.class.getName()).log(Level.SEVERE, null, e);
        }
    }
    
}
